package com.lanou.myapp.comic.themecomic;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class ThemeComicAdapter extends BaseAdapter {
    private List<ThemeComic> data;
    private Context context;



    public ThemeComicAdapter(Context context) {
        this.context = context;
//        init();

    }


    public void addData(List<ThemeComic> data) {
        this.data = data;
        notifyDataSetChanged();

    }

//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache  = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//
//        imageLoader = new ImageLoader(queue, imageCache);
//    }


    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size()/2 : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_theme_comic, null);
            myViewHolder = new MyViewHolder();
            myViewHolder.textView = (TextView) convertView.findViewById(R.id.tv_item_theme_comic);
            myViewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time_theme_comic);
            myViewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_item_theme_comic);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        ThemeComic themeComic = (ThemeComic) getItem(position);

//        String imgUrl = data.get(position).getSmall_cover();
//        getBitmapCache(imgUrl, myViewHolder.imageView);

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getSmall_cover(), listener ,280, 250);
//        myViewHolder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//        myViewHolder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//        myViewHolder.networkImageView.setImageUrl(data.get(position).getSmall_cover(), imageLoader);

//        Glide.with(context).load(data.get(position).getSmall_cover()).into(myViewHolder.imageView);



        myViewHolder.textView.setText(themeComic.getTitle());
        myViewHolder.tv_time.setText(String.valueOf(themeComic.getCreate_time()));
//        myViewHolder.textView.setText(data.get(position).getTitle());
//        myViewHolder.tv_time.setText(String.valueOf(data.get(position).getCreate_time()));

        return convertView;
    }

//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.img_lauch_bitch,
//                R.mipmap.img_lauch_bitch);
//        imageLoader.get(imgUrl, listener, 300, 300);
//    }


    class MyViewHolder {
        TextView textView;
        TextView tv_time;
        ImageView imageView;
        NetworkImageView networkImageView;
    }

}
